package com.framework.foundation.interceptor;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

public class MobileInterceptor implements Interceptor {

	/**
	 * 需要拦截的url
	 */
	private String urlPattern;

	/**
	 * 授权不拦截的地址
	 */
	private String[] allowUrls;

	public MobileInterceptor(String urlPattern) {
		this.urlPattern = urlPattern;
	}

	/**
	 * 
	 * constructor
	 * 
	 * @param urlPattern 需要拦截的地址
	 * @param allowUrls 授权不拦截的地址
	 * @Author: Brin
	 * @Date: 2015年10月21日 下午1:18:24
	 */
	public MobileInterceptor(String urlPattern, String[] allowUrls) {
		this.urlPattern = urlPattern;
		this.allowUrls = allowUrls;
	}

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		HttpServletRequest request = controller.getRequest();
		HttpServletResponse response = controller.getResponse();

		final String actionKey = inv.getActionKey();

		boolean result = false;
		if (ArrayUtils.isNotEmpty(allowUrls)) {
			// /判断是否为拦截的路径,不为拦截的路径直接通过
			result = CollectionUtils.exists(Arrays.asList(allowUrls), new Predicate() {
				@Override
				public boolean evaluate(Object input) {
					return StringUtils.contains(actionKey, (String) input);
				}
			});
		}

	}

}
